package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: TeachplanServiceImpl
 * Package: com.xuecheng.content.service.impl
 * Description:
 *
 * @Author Endeavor-ZQB
 * @Create 2023/1/30 19:03
 * @Version 1.0
 */
@Service
@Slf4j
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;


    @Override
    public List<TeachplanDto> findTeachplayTree(long courseId) {

        return teachplanMapper.selectTreeNodes(courseId);
    }

    //新增、修改
    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if (id!=null){
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else {
            Teachplan teachplan = new Teachplan();
            //找到同级课程计划的数量
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            //新的课程计划的值 排序号
            teachplan.setOrderby(count+1);

            BeanUtils.copyProperties(teachplanDto,teachplan);

            teachplanMapper.insert(teachplan);
        }

    }

    @Transactional
    @Override
    public void deleteTeachplanById(Long teachplanId) {
        teachplanMapper.deleteById(teachplanId);
    }



    @Transactional
    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划的id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        String mediaId = bindTeachplanMediaDto.getMediaId();

        //约束校验

        //教学计划不存在，无法绑定
        if (teachplan == null){
            XueChengPlusException.cast("教学计划不存在，无法绑定");
        }
        //只有二次目录才可以绑定视频
        Integer grade = teachplan.getGrade();
        if (grade != 2){
            XueChengPlusException.cast("只有二级目录才可以绑定视频");
        }


        //删除原来的绑定关系
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,teachplanId);
        teachplanMediaMapper.delete(queryWrapper);

        //添加新的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);

        return teachplanMedia;
    }

    @Override
    public void delAassociationMedia(Long teachPlanId, String mediaId) {
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,teachPlanId);
        queryWrapper.eq(TeachplanMedia::getMediaId,mediaId);
        teachplanMediaMapper.delete(queryWrapper);
    }

    //课程计划的上移和下移
    public void upDown(String index, Long teachPlanId) {
        //首先根据teachPlanId查询课程计划
        Teachplan teachplan1 = teachplanMapper.selectById(teachPlanId);
        //查询条件
        LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
        qw.eq(Teachplan::getParentid,teachplan1.getParentid())
                .eq(Teachplan::getCourseId,teachplan1.getCourseId())
                .gt(index.equals("movedown"),Teachplan::getOrderby,teachplan1.getOrderby()) //大于当前的序号
                .lt(index.equals("moveup"),Teachplan::getOrderby,teachplan1.getOrderby()) //小于当前的序号
                .orderByDesc(index.equals("moveup"),Teachplan::getOrderby) //降序
                .last(" limit 1");
        //根据条件查询课程计划
        Teachplan teachplan = teachplanMapper.selectOne(qw);
        if(teachplan == null) {
            XueChengPlusException.cast("到达边界，不能再移拉！");
        }
        //交换序号
        int o1 = teachplan1.getOrderby();
        int o2= teachplan.getOrderby();

        teachplan.setOrderby(o1);
        teachplan1.setOrderby(o2);

        teachplanMapper.updateById(teachplan1);
        teachplanMapper.updateById(teachplan);

    }


    //计算课程计划的orderby  找到同级课程计划的数量    select count(*) from teachplan where courseId=123 and parentId =  1;

    /**
     *  获取最新的排序号
     * @param courseId  课程id
     * @param parentId  父课程计划id
     * @return  int 最新排序号
     */
    public int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<Teachplan>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);

        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;

    }
}
